package com.thilux.servicemanager.validator;

import java.io.File;

import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;

import com.thilux.servicemanager.exception.BadConfigException;


public class ServiceConfigValidator {

    private static SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
    
    public static boolean isValid() throws BadConfigException{
	
	File xmlFile;
	File schemaFile;
	
	try{
	    xmlFile = new File("thlx-service-config.xml");
	    schemaFile = new File("thlx-service-config.xsd");
	    
	    Schema schema = schemaFactory.newSchema(schemaFile);
	    Validator validator = schema.newValidator();
	    Source source = new StreamSource(xmlFile);
	    
	    validator.validate(source);
	    return true;
	    
	}catch(Exception e){
	    throw new BadConfigException(e.getMessage());
	}
	
    }
    
}
